package com.findme.fecedetection.base

import android.os.Build
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.LinearLayout
import com.findme.fecedetection.R
import com.findme.fecedetection.utlis.ProgressDialog
import com.findme.fecedetection.utlis.ToastUtils
import com.tanguy.user.interfaces.RequestHelper
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import org.jetbrains.anko.find

/**
 * Created by dab on 2017/9/30 0030 09:03
 * 沉浸式
 */

abstract class ImmersionTitleActivity : AppCompatActivity(), RequestHelper {
    val rootLayout by lazy {
        find<LinearLayout>(R.id.root_layout)
    }

    @LayoutRes
    abstract fun setContentViewRes(): Int

    private val dialog: ProgressDialog by lazy {
        ProgressDialog(this, R.style.Theme_ProgressDialog)
    }
    val compositeDisposable = CompositeDisposable()
    override fun setContentView(layoutResID: Int) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE)//去掉标题栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        super.setContentView(R.layout.activity_base_title)
        if (layoutResID > 0) {
            layoutInflater.inflate(layoutResID, rootLayout, true)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(setContentViewRes())

        val status_bar: View = findViewById(R.id.view_status_bar)
        initStatusBar(status_bar)
        initView()
        initEvent()
        initData()
    }

    open fun initEvent() {}
    open fun initData() {}
    open fun initView() {

    }

    private fun initStatusBar(view: View) {
        var statusBarHeight = 0
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            //获取status_bar_height资源的ID
            val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
            if (resourceId > 0) {
                //根据资源ID获取响应的尺寸值
                statusBarHeight = resources.getDimensionPixelSize(resourceId)
            }
        }
        val layoutParams = view.layoutParams
        layoutParams.height = statusBarHeight
        view.layoutParams = layoutParams
    }


    override fun showToast(msg: String?) {
        ToastUtils.showToast(msg)
    }

    override fun showLoadDialog(msg: String, canCancel: Boolean) {
        showDialog(msg, false)
    }

    override fun dismissLoadDialog() {
        dismissDialog()
    }

    override fun cancelRequest(disposable: Disposable) {
        compositeDisposable.add(disposable)
    }

    fun showDialog(msg: String = "加载中...", canCancel: Boolean = true) {
        if (!canCancel) {
            dialog.setOnCancelListener {
                finish()
            }
        } else {
            //这里设置如果是可以取消的监听器置null了。可以自己在页面上重新设置想要的操作。这里不知道具体需求。
            dialog.setOnCancelListener(null)
        }
        dialog.setCanceledOnTouchOutside(canCancel)
        dialog.setMessage(msg)
        if (!dialog.isShowing) {
            dialog.show()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (!compositeDisposable.isDisposed) {
            compositeDisposable.dispose()
        }
    }

    fun dismissDialog() {
        if (dialog.isShowing) {
            dialog.dismiss()
        }
    }
}
